﻿/*==========================================
    *   程序说明: 防止HttpPost重复提交
    *   作者姓名: xp
    *   创建日期: 2106-05-12
    *   最后修改: 
    *   
    *   注: 
    *   版权所有特友 http://o2o.teshehui.com       
==========================================*/

using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace TY.UI.Mvc.Extension.ReSubmit
{
    public static class CommHtmlHelper
    {
        public static HtmlString GenerateVerficationToken(this HtmlHelper htmlhelper)
        {
            var formValue = Encrypt(HttpContext.Current.Session.SessionID + DateTime.Now.Ticks);
            HttpContext.Current.Session[PageTokenViewBase.SessionMyToken] = formValue;
            var fieldName = PageTokenViewBase.HiddenTokenName;
            var builder = new TagBuilder("input");
            builder.Attributes["type"] = "hidden";
            builder.Attributes["name"] = fieldName;
            builder.Attributes["value"] = formValue;
            return new HtmlString(builder.ToString(TagRenderMode.SelfClosing));
        }

        private static string Encrypt(string plaintext)
        {
            var cl1 = plaintext;
            var pwd = string.Empty;
            var md5 = MD5.Create();
            var s = md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
            return s.Aggregate(pwd, (current, t) => current + t.ToString("X"));
        }
    }
}
